package com.pig.waterfall;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class PigScrollView extends ScrollView {
	
	public interface OnPigScrollListener{
		public void onBottom();
		public void onTop();
		public void onScroll();
		public void onAutoScroll();
	}
	
	//
	
	private OnPigScrollListener onPigScrollListener;
	private View childView;
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.what){
			case 1:
				if(null != onPigScrollListener){
					if(childView.getMeasuredHeight() - 20 <= getScrollX() + getHeight()){
						onPigScrollListener.onBottom();
					}else if(getScrollY() == 0){
						onPigScrollListener.onTop();
					}else{
						onPigScrollListener.onScroll();
					}
				}
				break;
				
			default:
					break;
			}
		}
		
	};
	
	public PigScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		childView = getChildAt(0);
		
	}

	public PigScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		childView = getChildAt(0);
		
	}

	public PigScrollView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		childView = getChildAt(0);
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_UP:
			if(null != childView && null != onPigScrollListener){
				handler.sendMessageDelayed(handler.obtainMessage(1), 200);
			}
			break;

		default:
			break;
		}
		return false;
		//return super.onTouchEvent(ev);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		super.onScrollChanged(l, t, oldl, oldt);
		onPigScrollListener.onAutoScroll();
	}

	//

	public void setOnPigScrollListener(OnPigScrollListener onPigScrollListener){
		this.onPigScrollListener = onPigScrollListener;
	}
	
	
	
}
